#include "lcd.h"



void LCD_Send(char* str)
{
	uart_sendstr(str);
	uart_send(0xff);
	uart_send(0xff);
	uart_send(0xff);
}

void LCD_Init(void)
{
	char buff[16]={0};
	sprintf(buff,"num_t.x=300"); 
	LCD_Send(buff);
	sprintf(buff,"num_t.w=20"); 
	LCD_Send(buff);
}


//倒计时
void LCD_Show_Time(u8 time)
{
	char buff[24]={0};
	
	sprintf(buff,"num_t.val=%d",time); 
	LCD_Send(buff);
}

void LCD_Clear_Temp(void)
{
	char buff[32]={0};
	
	sprintf(buff,"cle 1,0"); 
	LCD_Send(buff);
	
	sprintf(buff,"cle 1,1"); 
	LCD_Send(buff);	

	sprintf(buff,"cle 1,2"); 
	LCD_Send(buff);	
	
	LCD_Show_Temp(0,0,0);
}

void LCD_Show_Temp(float temp,float hum,float adc)
{
	char buff[48]={0};
	
	int itemp = (int)(temp*10);
	int ihum  = (int)(hum*10);	
	int iadc  = (int)(adc*10);
	
	sprintf(buff,"txt_temp.txt=\"%d.%d\"",itemp/10,itemp%10); 
	LCD_Send(buff);
	
	sprintf(buff,"txt_hum.txt=\"%d.%d\"",ihum/10,ihum%10); 
	LCD_Send(buff);
	
	sprintf(buff,"txt_adc.txt=\"%d.%d\"",iadc/10,iadc%10); 
	LCD_Send(buff);	
	
}

#define HEIGHT  210.0
#define HUM_MAX 80.0
#define HUM_MIN 30.0

float TEMP_MAX=0;// = 30;
float TEMP_MIN=0;// = 20;


void LCD_Temp_Yaxis(u8 min,u8 max)
{
	char buff[16]={0};
	
	u8 step = (max-min)/5;
	
	TEMP_MAX = max;
	TEMP_MIN = min;
	
	//1
	sprintf(buff,"n0.val=%d",min+step*1); 
	LCD_Send(buff);
	
	//2
	sprintf(buff,"n1.val=%d",min+step*2); 
	LCD_Send(buff);

	//3
	sprintf(buff,"n2.val=%d",min+step*3); 
	LCD_Send(buff);

	//4
	sprintf(buff,"n3.val=%d",min+step*4); 
	LCD_Send(buff);	
}

u8 plot_array[2][320];

void LCD_Plot_Temp(float temp,float hum,float adc)
{
	char buff[16]={0};
	
	u8 plot_temp,plot_hum,plot_adc;
	
	LCD_Temp_Yaxis(18,33);
	
//	float max_temp = temp>adc?temp:adc;
//	
//	if(max_temp>35) LCD_Temp_Yaxis(24,40);
//	else if(max_temp>30) LCD_Temp_Yaxis(18,33);
//	else if(max_temp>25) LCD_Temp_Yaxis(20,30);
//	else LCD_Temp_Yaxis(15,25);
//	
	//temp(20~45℃)
	if(temp>TEMP_MAX) plot_temp=HEIGHT;
	else if(temp<TEMP_MIN)plot_temp=0;
	else plot_temp = (u8)((temp-TEMP_MIN)/(TEMP_MAX-TEMP_MIN)*HEIGHT);

	//hum(30~80%)
	if(hum>HUM_MAX) plot_hum=HEIGHT;
	else if(hum<HUM_MIN)plot_hum=0;
	else plot_hum = (u8)((hum-HUM_MIN)/(HUM_MAX-HUM_MIN)*HEIGHT);	
	
	//adc
	if(adc>TEMP_MAX) plot_adc=HEIGHT;
	else if(adc<TEMP_MIN)plot_adc=0;
	else plot_adc = (u8)((adc-TEMP_MIN)/(TEMP_MAX-TEMP_MIN)*HEIGHT);

	//cmd
	sprintf(buff,"add 1,0,%d",plot_temp); 
	LCD_Send(buff);
	
	sprintf(buff,"add 1,1,%d",plot_hum); 
	LCD_Send(buff);
	
	sprintf(buff,"add 1,2,%d",plot_adc); 
	LCD_Send(buff);
}
